<?php
class pro_kontrol_sertifikasi_model extends MY_Model {
	var $model = 'pro_kontrol_sertifikasi';
	
	function GetJSON()
	{
		if ($this->input->post('status') == 'expired') {
			$ls_sql	= "	SELECT	id_sertifikasi,kode_alat,nama_peralatan,nomor_sertifikasi,
								TO_CHAR(tgl_berlaku_awal,'DD/MM/YYYY') tgl_berlaku_awal,
								TO_CHAR(tgl_berlaku_akhir,'DD/MM/YYYY') tgl_berlaku_akhir
						FROM	PRO_KONTROL_SERTIFIKASI
						WHERE	ADD_MONTHS(TGL_BERLAKU_AKHIR, -1) <= SYSDATE
								and (UPPER(kode_alat) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(nama_peralatan) LIKE '%".strtoupper(post('keyword'))."%')
						ORDER BY ".(post('sort')=='' ? 'kode_alat' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		} else {
			$ls_sql	= "	SELECT	id_sertifikasi,kode_alat,nama_peralatan,nomor_sertifikasi,
								TO_CHAR(tgl_berlaku_awal,'DD/MM/YYYY') tgl_berlaku_awal,
								TO_CHAR(tgl_berlaku_akhir,'DD/MM/YYYY') tgl_berlaku_akhir
						FROM	PRO_KONTROL_SERTIFIKASI
						WHERE	(UPPER(kode_alat) LIKE '%".strtoupper(post('keyword'))."%'
								OR UPPER(nama_peralatan) LIKE '%".strtoupper(post('keyword'))."%')
						ORDER BY ".(post('sort')=='' ? 'kode_alat' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		}
		
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('pro_kontrol_sertifikasi', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('pro_kontrol_sertifikasi', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_kode_alat){
		$result = $this->db->get_where('pro_kontrol_sertifikasi', array('kode_alat' => $as_kode_alat));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kode_alat)
	{
		$result = $this->db->get_where('pro_kontrol_sertifikasi', array('kode_alat' => $as_kode_alat));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM pro_kontrol_sertifikasi WHERE kode_alat = '".$arr_rule['kode_alat']."'"))<=0)
			{
				return $this->error_delete.' [Id: '.$arr_rule['kode_alat']."]<br />";
			}else{
				$this->db->delete('pro_kontrol_sertifikasi', array('kode_alat' => $arr_rule['kode_alat']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GetCetakList(){
		
		$result = $this->db->query("  SELECT *
										FROM pro_kontrol_sertifikasi
										ORDER BY id_sertifikasi DESC");


		return $result->result_array($result);
		
	}
}
?>